<template>
  <card ref="Card">
    <div slot="header">
      <el-input placeholder="输入订单号搜索" v-model="searchNumber" class="w-200" clearable @keyup.enter.native="getData"/>
      <search-select-all @get="getSite" class="ml-5"/>
      <el-button type="success" @click="getData" class="ml-10">搜索</el-button>
      <el-button class="float-right" :disabled="selectedList.length===0" type="danger" @click="delDataMore" v-permission="['admin']">批量删除</el-button>
      <export-enter class="float-right"/>
    </div>
    <selection-table :data="formData" :keys.sync="selectedList">
      <el-table-column prop="number" label="订单号"></el-table-column>
      <el-table-column prop="username" label="关联网点"></el-table-column>
      <el-table-column prop="createTime" label="创建时间">
        <template slot-scope="scope">
          <span>{{scope.row.createTime | formatDateTime}}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="80">
        <template slot-scope="scope">
          <delete-button :ref="scope.row.id" :id="scope.row.id" @start="delData" v-permission="['admin']"/>
        </template>
      </el-table-column>
    </selection-table>
    <pagination ref="Pagination" @update="getData"/>
  </card>
</template>

<script>
  import {pageIncomingOrdersApi, delIncomingOrdersApi} from '@/api/warehouse'
  import {delNullKey} from "@/utils/common";
  import ExportEnter from '@/views/export/ExportEnter'
  import SearchSelectAll from '@/views/components/SearchSelectAll'

  export default {
    name: 'WarehouseList',
    components: {ExportEnter, SearchSelectAll},
    data() {
      return {
        formData: [],
        selectedList: [],
        searchNumber: '',
        searchUsername: ''
      }
    },
    mounted() {
      this.getData()
    },
    methods: {
      getSite(obj) {
        if (obj) {
          this.searchUsername = obj.username
        } else {
          this.searchUsername = ''
        }
        this.getData()
      },
      getData() {
        let pagination = this.$refs.Pagination;
        let param = {
          current: pagination.current,
          size: pagination.size,
          name: this.searchNumber,
          username: this.searchUsername
        };
        delNullKey(param)
        this.$refs.Card.start();
        pageIncomingOrdersApi(param).then(result => {
          let response = result.resultParam.incomingOrdersPage;
          this.formData = response.records;
          pagination.total = response.total;
          this.$refs.Card.stop();
        })
      },
      delData(id) {
        delIncomingOrdersApi({id: id})
          .then(() => {
            this.getData();
            this.$refs[id].close()
          })
          .catch(() => {
            this.$refs[id].stop();
          })
      },
      delDataMore: async function () {
        for (let i = 0; i < this.selectedList.length; i++) {
          await delIncomingOrdersApi({id: this.selectedList[i]})
        }
        this.getData();
      }
    }
  }
</script>
